package com.tankgame4;

import java.util.Vector;

import static java.lang.Thread.sleep;

public class EnemyTank extends Tank implements Runnable{
    Vector<Shot> shots = new Vector<>(); // 保存子弹对象
    //增加成员 enemyTank可以得到敌人坦克的Vector
    Vector<EnemyTank> enemyTanks = new Vector<>();
    public EnemyTank(int x, int y) {
        super(x, y);
        this.setDirect(2);
    }


    @Override
    public void run() {
        while (true) {
            if (isLive && shots.size() < 3) {
                Shot shot = null;

                switch (getDirect()) {
                    case 0:
                        shot = new Shot(getX() + 20, getY(), 0);
                        break;
                    case 1:
                        shot = new Shot(getX() + 60, getY() + 20, 1);
                        break;
                    case 2:
                        shot = new Shot(getX() + 20, getY() + 60, 2);
                        break;
                    case 3:
                        shot = new Shot(getX(), getY() + 20, 3);
                        break;
                }

                shots.add(shot);
                new Thread(shot).start();
            }

            // 获取敌方坦克方向 并继续机动
            switch (getDirect()) {
                case 0:
                    for (int i = 0; i < 30; i++) {
                        if (getY() > 0 && !isTouchEnemyTank()) {
                            moveUp();
                        }

                        // 休眠50ms
                        try {
                            sleep(50);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }

                    break;
                case 1:
                    for (int i = 0; i < 30; i++) {
                        if (getX() + 60 < 1000 && !isTouchEnemyTank()) {
                            moveRight();
                        }

                        // 休眠50ms
                        try {
                            sleep(50);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }

                    break;
                case 2:
                    for (int i = 0; i < 30; i++) {
                        if (getY() + 60 < 750 && !isTouchEnemyTank()) {
                            moveDown();
                        }

                        // 休眠50ms
                        try {
                            sleep(50);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }

                    break;
                case 3:
                    for (int i = 0; i < 30; i++) {
                        if (getX() > 0 && !isTouchEnemyTank()) {
                            moveLeft();
                        }

                        // 休眠50ms
                        try {
                            sleep(50);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }

                    break;
            }


            // 随即改变坦克方向
            setDirect((int)(Math.random() * 4));

            // 写并发程序 一定要考虑清楚 该线程的结束时间
            if (!isLive) {
                break;
            }

        }
    }

    // 将MyPanel对象的成员enemyTanks的引用赋给this.enemyTanks
    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    // 判断当前的enemyTank是否与其他的enemyTank发生碰撞
    public boolean isTouchEnemyTank() {
        // 判断当前敌人（this）坦克的方向
        switch (getDirect()) {
            case 0:
                // 让当前this坦克与其他坦克比较是否重叠
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    // 不和自己比较
                    if (this != enemyTank) {
                        // 如果敌人坦克是向上或向下
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            // 判断this坦克左上角是否进入enemyTank的边界
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            // 判断this坦克右上角是否进入enemyTank的边界
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        // 如果敌人坦克是向左或向右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            // 判断this坦克左上角是否进入enemyTank的边界
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            // 判断this坦克右上角是否进入enemyTank的边界
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 1:
                // 让当前this坦克与其他坦克比较是否重叠
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    // 不和自己比较
                    if (this != enemyTank) {
                        // 如果敌人坦克是向上或向下
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            // 判断this坦克右上角是否进入enemyTank的边界
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            // 判断this坦克右下角是否进入enemyTank的边界
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        // 如果敌人坦克是向左或向右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            // 判断this坦克右上角是否进入enemyTank的边界
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            // 判断this坦克右下角是否进入enemyTank的边界
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60 <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 2:
                // 让当前this坦克与其他坦克比较是否重叠
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    // 不和自己比较
                    if (this != enemyTank) {
                        // 如果敌人坦克是向上或向下
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            // 判断this坦克左下角是否进入enemyTank的边界
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 60) {
                                return true;
                            }
                            // 判断this坦克右下角是否进入enemyTank的边界
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 40
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        // 如果敌人坦克是向左或向右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            // 判断this坦克左下角是否进入enemyTank的边界
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 40) {
                                return true;
                            }
                            // 判断this坦克右下角是否进入enemyTank的边界
                            if (this.getX() + 40 >= enemyTank.getX()
                                    && this.getX() + 40 <= enemyTank.getX() + 60
                                    && this.getY() + 60 >= enemyTank.getY()
                                    && this.getY() + 60 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 3:
                // 让当前this坦克与其他坦克比较是否重叠
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    // 不和自己比较
                    if (this != enemyTank) {
                        // 如果敌人坦克是向上或向下
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2) {
                            // 判断this坦克左上角是否进入enemyTank的边界
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60) {
                                return true;
                            }
                            // 判断this坦克左下角是否进入enemyTank的边界
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 60) {
                                return true;
                            }
                        }
                        // 如果敌人坦克是向左或向右
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3) {
                            // 判断this坦克左上角是否进入enemyTank的边界
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40) {
                                return true;
                            }
                            // 判断this坦克左下角是否进入enemyTank的边界
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() + 40 >= enemyTank.getY()
                                    && this.getY() + 40 <= enemyTank.getY() + 40) {
                                return true;
                            }
                        }
                    }
                }
                break;
        }
        return false;
    }
}
